package org.amphiprion.montrictracv4.db.adapter;

import java.util.List;

import org.amphiprion.montrictracv4.App;
import org.amphiprion.montrictracv4.ApplicationConstants.DATA_TYPE;
import org.amphiprion.montrictracv4.R;
import org.amphiprion.montrictracv4.db.model.Game;
import org.amphiprion.montrictracv4.db.model.UserGameData;
import org.amphiprion.montrictracv4.fragment.GameListFragment;

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class GameListAdapter extends ArrayAdapter<Game> implements View.OnClickListener {

	FragmentActivity context;
	int layoutResourceId;
	List<Game> data = null;
	GameListFragment gameListFragment;

	public GameListAdapter(FragmentActivity paramFragmentActivity, GameListFragment gameListFragment, int layoutResourceId, List<Game> data) {
		super(paramFragmentActivity, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.gameListFragment = gameListFragment;
		this.context = paramFragmentActivity;
		this.data = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ItemHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new ItemHolder();
			holder.imgExtension = (ImageView) row.findViewById(R.id.imgExtension);
			holder.imgIcon = (ImageView) row.findViewById(R.id.imgIcon);

			holder.imgRefresh = (ImageView) row.findViewById(R.id.imgRefresh);
			holder.imgRefresh.setOnClickListener(this);
			holder.imgViewExtensions = (ImageView) row.findViewById(R.id.imgViewExtensions);
			holder.imgViewExtensions.setOnClickListener(this);
			holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
			holder.txtScore = (TextView) row.findViewById(R.id.txtScore);
			holder.txtPlayer = (TextView) row.findViewById(R.id.txtPlayers);
			holder.txtAge = (TextView) row.findViewById(R.id.txtAges);
			holder.txtDuration = (TextView) row.findViewById(R.id.txtDuration);

			holder.txtUserCarac1 = (TextView) row.findViewById(R.id.lblUserCarac1);
			holder.pgUserCarac1 = (ProgressBar) row.findViewById(R.id.pgUserCarac1);
			holder.txtUserCarac2 = (TextView) row.findViewById(R.id.lblUserCarac2);
			holder.pgUserCarac2 = (ProgressBar) row.findViewById(R.id.pgUserCarac2);

			row.setTag(holder);
		} else {
			holder = (ItemHolder) row.getTag();
		}

		Game item = data.get(position);
		holder.imgRefresh.setTag(item);
		holder.imgViewExtensions.setTag(item);
		holder.txtTitle.setText(item.name);

		// extension
		if (item.extension) {
			holder.imgExtension.setVisibility(View.VISIBLE);
		} else {
			holder.imgExtension.setVisibility(View.GONE);
		}

		// players
		String computedPlayersFromExtensions = null;
		if (item.minPlayer == item.c_minPlayer && item.maxPlayer == item.c_maxPlayer) {
			computedPlayersFromExtensions = "";
		} else {
			if (item.c_minPlayer == 0 && item.c_maxPlayer == 0) {
				computedPlayersFromExtensions = "";
			} else if (item.c_minPlayer == 0) {
				computedPlayersFromExtensions = " <- " + item.c_maxPlayer + ">";
			} else if (item.c_maxPlayer == 0) {
				computedPlayersFromExtensions = " <" + item.c_minPlayer + "+ >";
			} else {
				computedPlayersFromExtensions = " <" + item.c_minPlayer + " - " + item.c_maxPlayer + ">";
			}
		}
		if (item.minPlayer == 0 && item.maxPlayer == 0) {
			holder.txtPlayer.setText(context.getResources().getString(R.string.not_defined) + computedPlayersFromExtensions);
		} else if (item.minPlayer == 0) {
			holder.txtPlayer.setText(context.getResources().getString(R.string.players_max, item.maxPlayer) + computedPlayersFromExtensions);
		} else if (item.maxPlayer == 0) {
			holder.txtPlayer.setText(context.getResources().getString(R.string.players_min, item.minPlayer) + computedPlayersFromExtensions);
		} else {
			holder.txtPlayer.setText(context.getResources().getString(R.string.players_min_max, item.minPlayer, item.maxPlayer) + computedPlayersFromExtensions);
		}

		// ages
		if (item.ageMin == 0) {
			holder.txtAge.setText(context.getResources().getString(R.string.not_defined));
		} else {
			holder.txtAge.setText(context.getResources().getString(R.string.ages, item.ageMin));
		}
		// duration
		if (item.duration == 0) {
			holder.txtDuration.setText(context.getResources().getString(R.string.not_defined));
		} else {
			holder.txtDuration.setText(context.getResources().getString(R.string.duration, item.duration));
		}
		// image
		if (item.image != null) {
			App.setUrlDrawable(holder.imgIcon, item.image, context.getSupportFragmentManager());
		}
		// score
		if (item.nbVotes > 0) {
			holder.txtScore.setText("" + item.score + " / 10 (" + item.nbVotes + ")");
		} else {
			holder.txtScore.setText(context.getResources().getString(R.string.not_defined));
		}
		if (item._ugcForDisplay != null) {
			// userCarac1
			if (item._ugcForDisplay.size() >= 1) {
				UserGameData ugd = item._ugcForDisplay.get(0);
				if (ugd.typeForData == DATA_TYPE.FLOAT) {
					if ("USER_INFO".equals(ugd._table)) {
						holder.txtUserCarac1.setText(ugd.nameFromData + " : " + Math.round(Float.parseFloat(ugd.value)));
						holder.txtUserCarac1.setVisibility(View.VISIBLE);
						holder.pgUserCarac1.setVisibility(View.GONE);
					} else {
						holder.txtUserCarac1.setText(ugd.nameFromData);
						holder.pgUserCarac1.setProgress(Math.round(Float.parseFloat(ugd.value)));
						holder.txtUserCarac1.setVisibility(View.VISIBLE);
						holder.pgUserCarac1.setVisibility(View.VISIBLE);
					}
				} else {
					holder.txtUserCarac1.setText(ugd.nameFromData + " : " + ugd.value);
					holder.txtUserCarac1.setVisibility(View.VISIBLE);
					holder.pgUserCarac1.setVisibility(View.GONE);
				}
			} else {
				holder.txtUserCarac1.setVisibility(View.GONE);
				holder.pgUserCarac1.setVisibility(View.GONE);
			}
			// userCarac2
			if (item._ugcForDisplay.size() >= 2) {
				UserGameData ugd = item._ugcForDisplay.get(1);
				if (ugd.typeForData == DATA_TYPE.FLOAT) {
					if ("USER_INFO".equals(ugd._table)) {
						holder.txtUserCarac2.setText(ugd.nameFromData + " : " + Math.round(Float.parseFloat(ugd.value)));
						holder.txtUserCarac2.setVisibility(View.VISIBLE);
						holder.pgUserCarac2.setVisibility(View.GONE);
					} else {
						holder.txtUserCarac2.setText(ugd.nameFromData);
						holder.pgUserCarac2.setProgress(Math.round(Float.parseFloat(ugd.value)));
						holder.txtUserCarac2.setVisibility(View.VISIBLE);
						holder.pgUserCarac2.setVisibility(View.VISIBLE);
					}
				} else {
					holder.txtUserCarac2.setText(ugd.nameFromData + " : " + ugd.value);
					holder.txtUserCarac2.setVisibility(View.VISIBLE);
					holder.pgUserCarac2.setVisibility(View.GONE);
				}
			} else {
				holder.txtUserCarac2.setVisibility(View.GONE);
				holder.pgUserCarac2.setVisibility(View.GONE);
			}
		} else {
			holder.txtUserCarac1.setVisibility(View.GONE);
			holder.pgUserCarac1.setVisibility(View.GONE);
			holder.txtUserCarac2.setVisibility(View.GONE);
			holder.pgUserCarac2.setVisibility(View.GONE);
		}
		// holder.txtDateSynchro.setText(App.formatDate(item.lastSynchro));

		return row;
	}

	static class ItemHolder {
		ImageView imgExtension;
		ImageView imgIcon;
		ImageView imgRefresh;
		ImageView imgViewExtensions;
		TextView txtTitle;
		TextView txtScore;
		TextView txtPlayer;
		TextView txtAge;
		TextView txtDuration;
		TextView txtUserCarac1;
		ProgressBar pgUserCarac1;
		TextView txtUserCarac2;
		ProgressBar pgUserCarac2;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.imgRefresh) {
			Game game = ((Game) v.getTag());
			gameListFragment.refreshGame(game);
		} else if (v.getId() == R.id.imgViewExtensions) {
			Game game = ((Game) v.getTag());
			gameListFragment.viewGameExtension(game);
		}
	}
}
